$(function () {
    /*
     * 抽取所有需要用得元素.
     */
    var clientExploitationDatagrid, clientExploitationDialog, clientExploitationForm, clientExploitationSearchBtn, executePlanDialog, executePlanForm;
    clientExploitationDatagrid = $("#clientExploitation_datagrid");
    clientExploitationDialog = $("#clientExploitation_dialog");
    clientExploitationForm = $("#clientExploitation_form");
    clientExploitationSearchBtn = $("#searchBtn");
    executePlanDialog = $("#executePlan_dialog");
    executePlanForm = $("#executePlan_form");
    /*
     * 初始化数据表格
     */
    clientExploitationDatagrid.datagrid({
        url: "/clientExploitation/list",
        fit: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pagination: true,
        fitColumns: true,
        toolbar: '#clientExploitation_datagrid_tb',
        columns: [
            [
                {
                    field: 'developtype',
                    width: 10,
                    align: 'center',
                    title: '开发类型',
                    formatter: function (value, row, index) {
                        return value == 1 ? "潜在客户开发" : "开发潜在客户";
                    }
                },
                {
                    field: 'client',
                    width: 10,
                    align: 'center',
                    title: '潜在客户',
                    formatter: function (value, row, index) {
                        return value ? value.realname : "待开发";
                    }
                },
                {field: 'plantime', width: 10, align: 'center', title: '计划实施时间'},
                {field: 'plantheme', width: 10, align: 'center', title: '计划主题'},
                {field: 'plancontent', width: 10, align: 'center', title: '计划内容'},
                {field: 'remark', width: 10, align: 'center', title: '备注'},
                {field: 'createtime', width: 10, align: 'center', title: '创建时间'},
                {
                    field: 'status',
                    width: 10,
                    align: 'center',
                    title: '是否实施',
                    formatter: function (value, row, index) {
                        return value ? "是" : "否";
                    }
                },
                {
                    field: 'executetime',
                    width: 10,
                    align: 'center',
                    title: '实施时间',
                    formatter: function (value, row, index) {
                        return value ? value : "<font color=#a9a9a9'>实施后显示</font>";
                    }
                },
                {
                    field: 'executeresult',
                    width: 10,
                    align: 'center',
                    title: '实施效果',
                    formatter: function (value, row, index) {
                        return value ? value : "<font color=#a9a9a9'>实施后显示</font>";
                    }
                },
                {
                    field: 'create', width: 10, align: 'center', title: '创建人', formatter: function (value, row, index) {
                    return value ? value.realname : "未知";
                }
                },
            ]
        ]
    });
    clientExploitationSearchBtn.textbox({
        width: 230,
        label: "关键字:",
        labelWidth: 50,
        prompt: "请输入搜索关键字",
        buttonText: '搜索',
        buttonIcon: 'icon-search',
        onClickButton: function () {
            var keyword = $(this).val();
            clientExploitationDatagrid.datagrid("load", {
                keyword: keyword
            });
        }
    });
    /*
     * 初始化新增/编辑对话框
     */
    clientExploitationDialog.dialog({
        width: 300,
        height: 390,
        closed: true,
        buttons: '#clientExploitation_dialog_bt'
    });
    executePlanDialog.dialog({
        width: 300,
        height: 250,
        closed: true,
        buttons: '#executePlan_dialog_bt'
    });

    //开发类型为开发潜在客户时,隐藏潜在客户选择框
    $("#developtype_combobox").combobox({
        onChange: function (newValue, oldValue) {
            if (newValue == 0) {
                $("#div1").prop("style").display = "none";
                clientExploitationDialog.dialog("refresh");
            } else if (newValue == 1) {
                $("#div1").prop("style").display = "block";
                clientExploitationDialog.dialog("refresh");
            }
        }
    });
    /*
     * 对页面按钮进行统一监听
     */
    $("a[data-cmd]").on("click", function () {
        var cmd = $(this).data("cmd");
        if (cmd) {
            cmdObj[cmd]();
        }
    });
    /*
     * 所有的操作封装到cmdObj对象中,方便管理
     */
    var cmdObj = {
        //实施开发计划
        executePlan: function () {
            var rowData = clientExploitationDatagrid.datagrid("getSelected");
            if (rowData) {
                executePlanForm.form("clear");
                executePlanDialog.dialog("setTitle", "实施计划");
                executePlanDialog.dialog("open");
                executePlanForm.form("load", rowData);
            } else {
                $.messager.alert("温馨提示", "请选择需要实施的计划!", "warining");
            }
        },
        saveExecutePlan: function () {
            var url = "/clientExploitation/executePlan";
            executePlanForm.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            executePlanDialog.dialog("close");
                            clientExploitationDatagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            })
        },
        cancelExecutePlan: function () {
            executePlanDialog.dialog("close");
        },
        //增删改,
        add: function () {
            clientExploitationForm.form("clear");
            clientExploitationDialog.dialog("setTitle", "新增");
            clientExploitationDialog.dialog("open");
        },
        edit: function () {
            var rowData = clientExploitationDatagrid.datagrid("getSelected");
            if (rowData) {
                clientExploitationForm.form("clear");
                clientExploitationDialog.dialog("setTitle", "编辑");
                clientExploitationDialog.dialog("open");
                if (rowData.client) {
                    rowData["client.id"] = rowData.client.id;
                }
                clientExploitationForm.form("load", rowData);
            } else {
                $.messager.alert("温馨提示", "请选择需要编辑的数据!", "warining");
            }
        },
        del: function () {
            var rowData = clientExploitationDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要删除选中数据吗？", function (yes) {
                    if (yes) {
                        $.get("/clientExploitation/delete?clientExploitationId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    clientExploitationDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要删除的数据!", "warining");
            }
        },
        //刷新,确认,取消
        reload: function () {
            clientExploitationDatagrid.datagrid("reload");
        },
        save: function () {
            var url;
            var idVal = $("[name='id']").val();
            if (idVal) {
                url = "/clientExploitation/update";
            } else {
                url = "/clientExploitation/save";
            }
            clientExploitationForm.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            clientExploitationDialog.dialog("close");
                            clientExploitationDatagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            });
        },
        cancel: function () {
            clientExploitationDialog.dialog("close");
        }
    }
});
